<?php

namespace app\model;

use think\Model;

class Money extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'money';
    // 设置当前模型主键
    protected $pk = 'id';

    const TYPE_COMMON = 1;//普通充值
    const TYPE_ONE_YEAR_VIP = 2;//年卡会员
    const TYPE_HALF_YEAR_VIP = 3;//半年会员
    const TYPE_SEASON_VIP = 4;//季卡会员
    const TYPE_60_VIP = 5;//60天会员
    const TYPE_30_VIP = 6;//30天会员
    const TYPE_10_VIP = 7;//10天会员
    const TYPE_7_VIP = 8;//7天会员
    const TYPE_SIGN = 9;//签到
    const TYPE_SHARE = 10;//分享
    const TYPE_3_VIP = 11;//3天会员
    const TYPE_UNLOCK_ALL = 12;//解锁全集
    const TYPE_ACTIVITY = 13;//活动充值
    const TYPE_AD_SIGN = 14; //广告充值
    const TYPE_1_VIP = 15;  //1天会员
    //充值类型数组
    const TYPE_ARR = [
        self::TYPE_COMMON => '普通充值',
        self::TYPE_ONE_YEAR_VIP => '年卡会员',
        self::TYPE_HALF_YEAR_VIP => '半年会员',
        self::TYPE_SEASON_VIP => '季卡会员',
        self::TYPE_60_VIP => '60天会员',
        self::TYPE_30_VIP => '30天会员',
        self::TYPE_10_VIP => '10天会员',
        self::TYPE_7_VIP => '7天会员',
        self::TYPE_SIGN => '签到',
        self::TYPE_SHARE => '分享',
        self::TYPE_3_VIP => '3天会员',
        self::TYPE_UNLOCK_ALL => '解锁全集',
        self::TYPE_ACTIVITY => '活动充值',
        self::TYPE_AD_SIGN => '广告奖励',
        self::TYPE_1_VIP => '1天会员',
    ];

    //vip类型对应天数
    const TYPE_VIP_DAYS = [
        self::TYPE_ONE_YEAR_VIP => 365,
        self::TYPE_HALF_YEAR_VIP => 180,
        self::TYPE_SEASON_VIP => 90,
        self::TYPE_60_VIP => 60,
        self::TYPE_30_VIP => 30,
        self::TYPE_10_VIP => 10,
        self::TYPE_7_VIP => 7,
        self::TYPE_3_VIP => 3,
        self::TYPE_1_VIP => 1,
    ];

    /**
     * 获取充值类型文本
     * @param $payRecord
     * @return string
     */
    public static function getTypeText($payRecord): string
    {
        $type = $payRecord['type'] ?? self::TYPE_COMMON;
        $totalFee = $payRecord['total_fee'] ?? 0.01;
        switch ($type){
            //普通充值
            case self::TYPE_COMMON:
                $text = 'K币充值'.$totalFee;
                break;
            //年卡充值
            case self::TYPE_ONE_YEAR_VIP:
                $text = '年卡充值'.$totalFee;
                break;
            //签到
            case self::TYPE_SIGN:
                $text = '签到获得';
                break;
            //分享
            case self::TYPE_SHARE:
                $text = '分享获得';
                break;
            //解锁全集
            case self::TYPE_UNLOCK_ALL:
                $videoId = $payRecord['video_id'] ?? 0;
                $videoTitle = Videos::where('id',$videoId)->value('name') ?? '';
                $text = '解锁全集-'."《{$videoTitle}》";
                break;
            //活动充值
            case self::TYPE_ACTIVITY:
                $text = $payRecord['name'] ?? '';
                break;
            case self::TYPE_AD_SIGN:
                $text = '广告奖励';
                break;
            default:
                $text = '会员充值'.$totalFee;
                break;
        }
        return $text;
    }

    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param array $where
     * @param string fields
     * @return array
     */
    public function getDataByWhere(array $where, string $fields = 'id, name, label, original_currency, send_currency, sum_currency, send_title', $order = 'sort asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    // 获取分页数据
    public function getPagingDataByWhere(array $where, int $page = 1, int $limit = 10, string $fields = 'id, name', string $order = 'id desc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) return ['total' => 0, 'total_page' => 0, 'data' => []];

        $offset = ($page - 1) * $limit;

        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    // 根据id获取数据
    public function getInfoById(int $id, string $fields = 'id, name')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 新增/更新
     * 
     * @dateTime 2022/08/24
     * @author zhixiang0215@126.com
     * @param array $params
     * @return int
     */
    public function edit($params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    // 根据id获取数据
    public function getInfoByWhere(array $where, string $fields = 'id, name')
    {
        $data = $this::where($where)->field($fields)->find();
        return $data ? $data->toArray() : [];
    }
}
